<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章列表</span>
      </div>
      <!-- 搜索区域 -->
      <div class="search-box">
        <el-form :inline="true" :model="q">
          <el-form-item label="文章分类">
            <el-select v-model="q.cate_id" placeholder="请选择分类" size="small">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发布状态" style="margin-left: 15px;">
            <el-select v-model="q.state" placeholder="请选择状态" size="small">
              <el-option label="已发布" value="已发布"></el-option>
              <el-option label="草稿" value="草稿"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small">筛选</el-button>
            <el-button type="info" size="small">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 发表文章的按钮 -->
        <el-button type="primary" size="small" class="btn-pub" @click="pubDialogVisible=true">发表文章</el-button>
      </div>

      <!-- 文章表格区域 -->
    <el-dialog
     title="发表文章"
     :visible.sync="pubDialogVisible"
     fullscreen
     :before-close="handleClose"
     >
     <el-form ref="pushArticleRules" :rules="pushArticleRules" :model="pushArticle" label-width="80px">
      <el-form-item label="文章标题" prop="title" >
        <el-input v-model="pushArticle.title" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select style="width:100%" v-model="pushArticle.cate_id" placeholder="请选择分类">
          <el-option :label="item.cate_name" :value="item.id" v-for="item in cateList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <!-- 使用 v-model 进行双向的数据绑定 -->
        <quill-editor v-model="pushArticle.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面">
        <!-- 用来显示封面的图片 -->
        <img src="../../../assets/images/cover.jpg" alt="" class="cover-img" ref="imgRef" />

        <br />
        <!-- 文件选择框，默认被隐藏 -->
        <input type="file" style="display: none;" @change="getImg" accept="image/*" ref="iptFile" />
        <!-- 选择封面的按钮 -->
        <el-button type="text" @click="$refs.iptFile.click()">+ 选择封面</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="pubArticle('已发布')" >发布</el-button>
        <el-button type="info" @click="pubArticle('草稿')">存为草稿</el-button>
      </el-form-item>
      </el-form>

  	<span>这是一段信息</span>
    </el-dialog>
      <!-- 分页区域 -->
    </el-card>
  </div>
</template>

<script>
import moduleImg from '@/assets/images/cover.jpg'
export default {
  name: 'ArtList',
  data() {
    return {
      // 查询参数对象
      pubDialogVisible:false,
      pushArticle:{
        title:'',
        cate_id:'',
        content:'',
        cover_img:'',
        state:'',
      },
      cateList:[],
      pushArticleRules:{
        title:[ { required: true, message: '请输入文章标题', trigger: 'blur' },],
        cate_id:[ { required: true, message: '请输入文章分类', trigger: 'blur' },],
        content:[{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      q: {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
      }
    }
  },
  methods:{
    handleClose(){
      this.$confirm('您确定要关闭吗').then(()=>this.pubDialogVisible=false)
    },
    getImg(e){
      if(!e.target.files) {
       this.pushArticle.cover_img = ""
        return this.$refs.imgRef.src = moduleImg
      }
      this.pushArticle.cover_img = e.target.files[0]
      this.$refs.imgRef.src = URL.createObjectURL(e.target.files[0])
      console.log(this.pushArticle.cover_img );
      },
    pubArticle(state){
      this.pushArticle.state=state
      this.$refs.pushArticleRules.validate(async (vali)=>{
        if(!vali) return
        if(!this.pushArticle.cover_img) return this.$message.error('请上传封面')
        const fd = new FormData
        console.log(fd);
        // fd.append('title',this.pushArticle.title)
        // fd.append('cate_id',this.pushArticle.cate_id)
        // fd.append('content',this.pushArticle.content)
        // fd.append('cover_img',this.pushArticle.cover_img)
        // fd.append('state',this.pushArticle.state)
        Object.keys(this.pushArticle).forEach(item=>{
          fd.append(item,this.pushArticle[item])
        })
        console.log(fd);
        const {data:res} = await this.$http.post('/my/article/add',fd)
        console.log(res);
      })
    }
  },
  async created(){
  const {data:res} = await this.$http.get('/my/cate/list')
  this.cateList= res.data
  console.log(res);
  },
}
</script>

<style lang="less" scoped>
.search-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .btn-pub {
    margin-top: 5px;
  }
}
/deep/ .ql-editor{
  min-height: 300px;
}
/deep/.ql-toolbar.ql-snow {
  line-height: 0;
}

   // 设置图片封面的宽高
   .cover-img {
     width: 400px;
     height: 280px;
     object-fit: cover;
   }


</style>
